<template>
  <div class="login">
    <div class="login-container">
      <div class="login-content">
        <div class="login-header">
          <h2>HIS医院信息系统</h2>
          <p class="subtitle">专业的医院管理解决方案</p>
        </div>
        <el-card class="login-card" shadow="never">
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="rules"
            @submit.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入用户名"
                :prefix-icon="User"
                size="large"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
                size="large"
              />
            </el-form-item>
            <div class="form-footer">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <a href="#" class="forgot-password">忘记密码？</a>
            </div>
            <el-form-item>
              <el-button 
                type="primary" 
                native-type="submit"
                :loading="loading"
                class="login-button"
                size="large"
              >
                登录
              </el-button>
            </el-form-item>
            <div class="register-link">
              还没有账号？
              <el-link 
                type="primary" 
                @click="handleRegister"
                class="register-button"
              >
                立即注册
              </el-link>
            </div>
          </el-form>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6-20个字符之间', trigger: 'blur' }
  ]
}

// 表单引用
const loginFormRef = ref<FormInstance>()

// 加载状态
const loading = ref(false)
const rememberMe = ref(false)

// 处理注册
const handleRegister = () => {
  router.push('/register')
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    // 表单验证
    const valid = await loginFormRef.value.validate()
    if (!valid) {
      ElMessage.warning('请填写完整的登录信息')
      return
    }
    
    loading.value = true
    
    // 调用 store 的 login action
    const success = await userStore.login(loginForm.username, loginForm.password)
    
    if (success) {
      ElMessage.success('登录成功')
      
      // 根据用户角色决定跳转路径
      const userRole = userStore.primaryRole
      let redirectPath = '/'
      
      if (userRole === 'doctor') {
        redirectPath = '/doctor/outpatient/diagnosis'
      } else if (userRole === 'nurse') {
        redirectPath = '/nurse/outpatient/registration'
      } else if (userRole === 'admin') {
        redirectPath = '/system/users'
      }
      
      await router.replace(redirectPath)
    } else {
      ElMessage.error('登录失败，请检查用户名和密码')
    }
  } catch (error: any) {
    console.error('Login error:', error)
    ElMessage.error(error?.message || '登录过程中发生错误')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login {
  min-height: 100vh;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-content {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  padding: 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #303133;
  margin: 0;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

.login-card {
  border: none;
  background: transparent;
  box-shadow: none !important;
}

.login-card :deep(.el-card__body) {
  padding: 0;
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forgot-password {
  color: #409EFF;
  text-decoration: none;
  font-size: 14px;
}

.login-button {
  width: 100%;
}

.register-link {
  text-align: center;
  margin-top: 15px;
  color: #606266;
  font-size: 14px;
}

.register-button {
  padding: 0 5px;
  font-weight: 500;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}
</style> 